package com.ftwj.demo.modular.controller;

import java.util.List;
import java.util.Map;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.Positive;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ftwj.demo.base.util.PageInfoBean;
import com.ftwj.demo.config.swagger.DataType;
import com.ftwj.demo.config.swagger.HttpMethod;
import com.ftwj.demo.config.swagger.ParamType;
import com.ftwj.demo.modular.entity.AreaBean;
import com.ftwj.demo.modular.service.AreaService;
import com.ftwj.demo.utils.result.MessageEnum;
import com.ftwj.demo.utils.result.ResultBean;
import com.ftwj.demo.utils.validate.ValidateGroupKey;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
* <p>Title: AreaController</p>  
* <p>Description: 地区前端控制器</p>
* <p>Copyright: Copyright (c) 2020 </p>  
* <p>Company: 兰州飞天网景信息产业有限公司</p>    
* @author HeYang
* @date 2020年4月10日 下午6:50:08
* @version V1.0
 */
@RestController 
@RequestMapping("/area")
@Api(tags="区域管理接口", value="区域部门管理")
@Validated
public class AreaController {

	@Autowired
	private AreaService service;
	
	/**
	 * <p>Title: tree</p>  
	 * <p>Description: 地区树数据</p>  
	 * @param pid
	 * @param size
	 * @author HeYang
	 * @date 2020年4月10日  
	 * @version 1.0
	 */
	@GetMapping("tree")
	@ApiOperation(value="地区树数据", notes="数据",code=0,httpMethod=HttpMethod.GET)
	@ApiImplicitParams({
		 @ApiImplicitParam(name="pid", value="父ID", dataType=DataType.INTEGER, paramType=ParamType.QUERY, defaultValue="0")
		,@ApiImplicitParam(name="size", value="查询深度", dataType=DataType.INTEGER, paramType=ParamType.QUERY, defaultValue="10")
	})
	public ResultBean<?> tree(@RequestParam(required = true,defaultValue = "000000") Integer pid, @RequestParam(required = true, defaultValue = "1") Integer size) {
		List<Map<String, Object>> list = service.findlistMap(pid, size);
		return ResultBean.success(list);
	}
	
	/**
	 * <p>Title: page</p>  
	 * <p>Description: 地区分页查询</p>  
	 * @param pid
	 * @param current
	 * @param size
	 * @author HeYang
	 * @date 2020年5月6日  
	 * @version 1.0
	 */
	@GetMapping("page")
	@ApiOperation(value="地区分页查询", notes="数据",code=0,httpMethod=HttpMethod.GET)
	@ApiImplicitParams({
		 @ApiImplicitParam(name="pid", value="父ID", dataType=DataType.STRING, paramType=ParamType.QUERY, defaultValue="000000")
		,@ApiImplicitParam(name="current", value="当前页数", dataType=DataType.INTEGER, paramType=ParamType.QUERY, defaultValue="1")
		,@ApiImplicitParam(name="size", value="查询深度", dataType=DataType.INTEGER, paramType=ParamType.QUERY, defaultValue="10")
	})
	public ResultBean<?> page(@RequestParam(required = true) String pid,@RequestParam(required = true) Integer current,@RequestParam(required = true) Integer size) {
		PageInfoBean pageInfo = new PageInfoBean();
		pageInfo.setPerPage(size).setPerPage(size);
		pageInfo = service.page(pageInfo,pid);
		return ResultBean.success(pageInfo);
	}
	
	/**
	 * <p>Title: list</p>  
	 * <p>Description: 地区列表查询</p>  
	 * @param pid
	 * @author HeYang
	 * @date 2020年5月6日  
	 * @version 1.0
	 */
	@GetMapping("list/{pid}")
	@ApiOperation(value="地区列表查询", notes="数据",code=0,httpMethod=HttpMethod.GET)
	@ApiImplicitParams({
		 @ApiImplicitParam(name="pid", value="父ID", dataType=DataType.STRING, paramType=ParamType.PATH, defaultValue="000000")
	})
	public ResultBean<?> list(@PathVariable@Positive String pid) {
		List<AreaBean> list = service.list(pid);
		return ResultBean.success(list);
	}
	
	/**
	 * <p>Title: insert</p>  
	 * <p>Description: 地区新增</p>  
	 * @param area
	 * @return 
	 * @author HeYang
	 * @date 2020年4月10日  
	 * @version 1.0
	 */
	@PostMapping("/insert")
	@ApiOperation(value="地区新增", notes="数据",code=0,httpMethod=HttpMethod.POST)
	public ResultBean<?> insert(@RequestBody@Validated AreaBean area) {
		if(service.add(area)>0) {
			return ResultBean.success();
		}else {
			return ResultBean.error(MessageEnum.SERVER_FAILED);
		}
	}
	
	/**
	 * <p>Title: update</p>  
	 * <p>Description: 地区修改</p>  
	 * @param area
	 * @return 
	 * @author HeYang
	 * @date 2020年4月10日  
	 * @version 1.0
	 */
	@PutMapping("/update")
	@ApiOperation(value="地区修改", notes="数据",code=0,httpMethod=HttpMethod.PUT)
	public ResultBean<?> update(@RequestBody@Validated(ValidateGroupKey.class) AreaBean area) {
		if(service.save(area)>0) {
			return ResultBean.success();
		}else {
			return ResultBean.error(MessageEnum.SERVER_FAILED);
		}
	}
	
	/**
	 * <p>Title: info</p>  
	 * <p>Description: 查看详情</p>  
	 * @param id
	 * @author HeYang
	 * @date 2020年4月29日  
	 * @version 1.0
	 */
	@GetMapping("/info/{id}")
	@ApiOperation(value="查看详情", notes="数据",code=0,httpMethod=HttpMethod.GET)
	@ApiImplicitParams({
		@ApiImplicitParam(name="id", value="地区id", dataType=DataType.STRING, paramType=ParamType.PATH)
	})
	public ResultBean<?> info(@NotEmpty@PathVariable String id) {
		AreaBean area = service.find(id);
		if(area != null) {
			return ResultBean.success(area);
		}else {
			return ResultBean.error(MessageEnum.SERVER_FAILED);
		}
	}
}
